const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");

const isDebug = process.env.MODE != "production";

/**@type {webpack.Configuration} */
const config = {
  entry: "./src/main.js",
  output: {
    path: path.resolve("dist"),
    filename: "[name].js",
    clean: true,
  },
  optimization: {
    minimize: !isDebug,
  },
  module: {
    rules: [
      {
        test: /\.(png|jpg|jpeg|gif)$/,
        type: "asset",
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader"],
      },
    ],
  },

  externals: ["axios", "jquery"],
  mode: isDebug ? "development" : "production",
  devServer: {
    port: 80,
    host: "127.0.0.1",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "src/index.ejs",
      inject: "head",
    }),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: "src/static",
          to: "static",
        },
      ],
    }),
  ],
};

module.exports = config;
